<?php

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\EmailPreferencesAction;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Webtrees;

/**
 * @var string   $DKIM_DOMAIN
 * @var string   $DKIM_KEY
 * @var string   $DKIM_SELECTOR
 * @var string   $SMTP_ACTIVE
 * @var string   $SMTP_AUTH
 * @var string   $SMTP_AUTH_USER
 * @var string   $SMTP_FROM_NAME
 * @var string   $SMTP_HELO
 * @var string   $SMTP_HOST
 * @var string   $SMTP_PORT
 * @var string   $SMTP_SSL
 * @var string[] $mail_ssl_options
 * @var string[] $mail_transport_options
 * @var bool     $smtp_helo_valid
 * @var bool     $smtp_from_name_valid
 * @var string   $title
 */
?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), $title]]) ?>

<h1><?= $title ?></h1>

<div class="alert alert-info">
    <?= I18N::translate('See %s for more information.', '<a class="alert-link" href="' . e(Webtrees::URL_FAQ_EMAIL) . '">' . e(Webtrees::URL_FAQ_EMAIL) . '</a>') ?>
</div>

<form method="post" action="<?= e(route(EmailPreferencesAction::class)) ?>" class="form-horizontal">
    <?= csrf_field() ?>

    <div class="row form-group">
        <label for="SMTP_ACTIVE" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Messages') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['name' => 'SMTP_ACTIVE', 'selected' => $SMTP_ACTIVE, 'options' => $mail_transport_options]) ?>
            <p class="small text-muted">
                <?= /* I18N: Help text for the “Messages” site configuration setting */
                I18N::translate('webtrees needs to send emails, such as password reminders and website notifications.') ?>
            </p>
        </div>
    </div>

    <div class="row form-group">
        <label for="SMTP_FROM_NAME" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Sender name') ?>
        </label>
        <div class="col-sm-9">
            <input type="email" class="form-control" id="SMTP_FROM_NAME" name="SMTP_FROM_NAME" value="<?= e($SMTP_FROM_NAME) ?>" maxlength="255">
            <p class="small text-muted">
                <?= /* I18N: Help text for the “Sender name” site configuration setting */
                I18N::translate('This name is used in the “From” field, when sending automatic emails from this server.') ?>
            </p>

            <?php if (!$smtp_from_name_valid) : ?>
                <p class="alert alert-warning">
                    <?= I18N::translate('Most mail servers require a valid email address.') ?>
                </p>
            <?php endif ?>
        </div>
    </div>

    <h2><?= I18N::translate('SMTP mail server') ?></h2>

    <div class="row form-group">
        <label for="SMTP_HOST" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Server name') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_HOST" name="SMTP_HOST" value="<?= e($SMTP_HOST) ?>" placeholder="smtp.example.com" maxlength="255" pattern="[a-z0-9-]+(\.[a-z0-9-]+)*">
            <p class="small text-muted">
                <?= /* I18N: Help text for the “Server name” site configuration setting */
                I18N::translate('This is the name of the SMTP server. “localhost” means that the mail service is running on the same computer as your web server.') ?>
            </p>
        </div>
    </div>

    <div class="row form-group">
        <label for="SMTP_PORT" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Port number') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_PORT" name="SMTP_PORT" value="<?= e($SMTP_PORT) ?>" pattern="[0-9]*" placeholder="25" maxlength="5">
            <p class="small text-muted">
                <?= /* I18N: Help text for the "Port number" site configuration setting */
                I18N::translate('By default, SMTP works on port 25.') ?>
            </p>
        </div>
    </div>

    <fieldset class="form-group">
        <div class="row">
            <legend class="col-form-label col-sm-3">
                <?= /* I18N: A configuration setting */
                I18N::translate('Use password') ?>
            </legend>
            <div class="col-sm-9">
                <?= view('components/radios-inline', ['name' => 'SMTP_AUTH', 'options' => [I18N::translate('no'), I18N::translate('yes')], 'selected' => (int) $SMTP_AUTH]) ?>
                <p class="small text-muted">
                    <?= /* I18N: Help text for the “Use password” site configuration setting */
                    I18N::translate('Most SMTP servers require a password.') ?>
                </p>
            </div>
        </div>
    </fieldset>

    <div class="row form-group">
        <label for="SMTP_AUTH_USER" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Username') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_AUTH_USER" name="SMTP_AUTH_USER" value="<?= e($SMTP_AUTH_USER) ?>" maxlength="255">
            <p class="small text-muted">
                <?= /* I18N: Help text for the "Username" site configuration setting */
                I18N::translate('The username required for authentication with the SMTP server.') ?>
            </p>
        </div>
    </div>

    <div class="row form-group">
        <label for="SMTP_AUTH_PASS" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Password') ?>
        </label>
        <div class="col-sm-9">
            <input type="password" class="form-control" id="SMTP_AUTH_PASS" name="SMTP_AUTH_PASS" value="" autocomplete="new-password">
            <p class="small text-muted">
                <?= /* I18N: Help text for the "Password" site configuration setting */
                I18N::translate('The password required for authentication with the SMTP server.') ?>
            </p>
        </div>
    </div>

    <div class="row form-group">
        <label for="SMTP_SSL" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Secure connection') ?>
        </label>
        <div class="col-sm-9">
            <?= view('components/select', ['name' => 'SMTP_SSL', 'selected' => $SMTP_SSL, 'options' => $mail_ssl_options]) ?>
            <p class="small text-muted">
                <?= /* I18N: Help text for the “Secure connection” site configuration setting */
                I18N::translate('Most servers do not use secure connections.') ?>
            </p>
        </div>
    </div>

    <div class="row form-group">
        <label for="SMTP_HELO" class="col-sm-3 col-form-label">
            <?= /* I18N: A configuration setting */
            I18N::translate('Sending server name') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="SMTP_HELO" name="SMTP_HELO" value="<?= e($SMTP_HELO) ?>" maxlength="255" pattern="[a-z0-9-]+(\.[a-z0-9-]+)*">
            <p class="small text-muted">
                <?= /* I18N: Help text for the "Sending server name" site configuration setting */
                I18N::translate('Most mail servers require that the sending server identifies itself correctly, using a valid domain name.') ?>
            </p>

            <?php if (!$smtp_helo_valid) : ?>
                <p class="alert alert-warning">
                    <?= I18N::translate('Most mail servers require a valid domain name.') ?>
                </p>
            <?php endif ?>
        </div>
    </div>

    <h2>
        <?= /* I18N: https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail */
        I18N::translate('DKIM digital signature') ?>
    </h2>

    <div class="row form-group">
        <label for="DKIM_DOMAIN" class="col-sm-3 col-form-label">
            <?= I18N::translate('Domain name') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="DKIM_DOMAIN" name="DKIM_DOMAIN" value="<?= e($DKIM_DOMAIN) ?>" maxlength="255">
        </div>
    </div>

    <div class="row form-group">
        <label for="DKIM_SELECTOR" class="col-sm-3 col-form-label">
            <?= I18N::translate('Selector') ?>
        </label>
        <div class="col-sm-9">
            <input type="text" class="form-control" id="DKIM_SELECTOR" name="DKIM_SELECTOR" value="<?= e($DKIM_SELECTOR) ?>" maxlength="255">
        </div>
    </div>

    <div class="row form-group">
        <label for="DKIM_KEY" class="col-sm-3 col-form-label">
            <?= I18N::translate('Private key') ?>
        </label>
        <div class="col-sm-9">
            <textarea class="form-control" id="DKIM_KEY" name="DKIM_KEY"><?= e($DKIM_KEY) ?></textarea>
        </div>
    </div>

    <hr>

    <div class="row form-group">
        <div class="offset-sm-3 col-sm-9">
            <p class="form-check">
                <input class="form-check-input" type="checkbox" id="test" name="test">
                <label class="form-check-label" for="test">
                    <?= I18N::translate('Send a test email using these settings') ?>
                </label>
            </p>

            <button type="submit" class="btn btn-primary">
                <?= view('icons/save') ?>
                <?= I18N::translate('save') ?>
            </button>

            <a href="<?= e(route(ControlPanel::class)) ?>" class="btn btn-secondary">
                <?= view('icons/cancel') ?>
                <?= I18N::translate('cancel') ?>
            </a>
        </div>
    </div>
</form>

<?php View::push('javascript') ?>
<script>
    $("#SMTP_AUTH_PASS").hideShowPassword("infer", true);
</script>
<?php View::endpush() ?>
